package web.servlet.question;

import com.fasterxml.jackson.databind.ObjectMapper;
import domain.LyyBaseResponse;
import service.QuestionService;
import service.impl.QuestionServiceImpl;
import utils.LyyLogUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteQuestion")
public class DeleteQuestionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        String questionId = request.getParameter("questionId");
        QuestionService service=new QuestionServiceImpl();
        Exception exception=null;
        boolean b=false;
        try {
            b = service.deleteQuestion(Integer.parseInt(questionId));
        } catch (Exception e) {
            LyyLogUtil.logeE(e.getMessage());
            exception=e;
        }
        LyyBaseResponse lyyBaseResponse=new LyyBaseResponse();
        if(exception!=null){
            lyyBaseResponse.setErrMsg(exception.getMessage());
            lyyBaseResponse.setStatus(0);
        }else {
            if(b){
                lyyBaseResponse.setStatus(1);
            }else {
                lyyBaseResponse.setStatus(0);
                lyyBaseResponse.setErrMsg("删除失败！");
            }
        }
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.writeValue(response.getOutputStream(),lyyBaseResponse);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
